import os

from fastapi import FastAPI, Request
from starlette.staticfiles import StaticFiles
from starlette.templating import Jinja2Templates

from .taobao import taobao_router
from .sms import sms_router

app = FastAPI()
static_dir = os.path.join(os.path.dirname(__file__), '../static')
app.mount('/static', StaticFiles(directory=static_dir), name='static')
templates = Jinja2Templates(directory="templates")
app.include_router(taobao_router)
app.include_router(sms_router)


@app.get("/", include_in_schema=False)
async def root(request: Request):
    hostname = request.url.hostname
    users = [
        {'name': '接口：', 'url': 'http://{}:8000/docs'.format(hostname)},
        {'name': '文档：', 'url': 'http://{}:9090'.format(hostname)},
        {'name': '数据：', 'url': 'http://{}:8080'.format(hostname)}
    ]
    return templates.TemplateResponse("index.html", context={"request": request, "users": users})
